package com.lian.community.service.follow;

import com.lian.community.constant.RedisKeyType;
import com.lian.community.dao.redis.JedisDAO;
import com.lian.community.util.RedisKeyUtil;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Transaction;
import java.util.List;


/**
 * 关注是用的zset来存的，可以对关注的时间进行排序，然后列出来
 * 也可以用来求交集，并集
 */
@Mapper
public class FollowService {
    @Autowired
    JedisDAO jedisDAO;

    public boolean cancelfollow(int userId, int entityId, int entityType){
        String followerKey = RedisKeyUtil.get(entityId, entityType, RedisKeyType.FOLLOWER);
        String followeeKey = RedisKeyUtil.get(userId, entityType, RedisKeyType.FOLLOWEE);
        Transaction tx = jedisDAO.multi();
        Double time =(double) System.currentTimeMillis();
        tx.zrem(followerKey, String.valueOf(userId));
        tx.zrem(followeeKey, String.valueOf(entityId));
        List<Object> result = jedisDAO.getTx(tx);
        return result.size() == 2 && (Long)result.get(0) > 0 && (Long)result.get(1) > 0;

    }

    public boolean follow(int userId, int entityId, int entityType){
        String followerKey = RedisKeyUtil.get(entityId, entityType, RedisKeyType.FOLLOWER);
        String followeeKey = RedisKeyUtil.get(userId, entityType, RedisKeyType.FOLLOWEE);
        Transaction tx = jedisDAO.multi();
        Double time =(double) System.currentTimeMillis();
        // 实体的粉丝增加当前用户
        tx.zadd(followerKey, time, String.valueOf(userId));
        tx.zadd(followeeKey, time, String.valueOf(entityId));
        List<Object> result = jedisDAO.getTx(tx);
        return result.size() == 2 && (Long)result.get(0) > 0 && (Long)result.get(1) > 0;

    }

    /**
     * 返回user对某type的entity的总关注数
     * @param userId
     * @param entityType
     * @return
     */
    public Long countFollow(int userId, int entityType){
        String folloeeKey = RedisKeyUtil.get(userId, entityType, RedisKeyType.FOLLOWER);
        return jedisDAO.zcard(folloeeKey);
    }



    /**
     * 返回总粉丝数
     * @return
     */
    public Long countFans(int entityId, int entityType){
        String followerKey = RedisKeyUtil.get(entityId, entityType, RedisKeyType.FOLLOWER);
        return jedisDAO.zcard(followerKey);
    }

    /**
     * user是否关注了entity
     * @param userId
     * @param entityId
     * @param entityType
     * @return
     */
    public boolean whetherFollowee(int userId, int entityId, int entityType){
        String followeeKey = RedisKeyUtil.get(userId, entityType, RedisKeyType.FOLLOWEE);
        Double score = jedisDAO.zscore(followeeKey, String.valueOf(entityId));
        return score == null;

    }

    /**
     * user是否为entity的粉丝
     * @param userId
     * @param entityId
     * @param entityType
     * @return
     */
    public boolean whetherFan(int userId, int entityId, int entityType){
        String followerKey = RedisKeyUtil.get(entityId, entityType, RedisKeyType.FOLLOWER);
        Double score = jedisDAO.zscore(followerKey, String.valueOf(userId));
        return score == null;

    }


    /**
     * 获得关注某一个type的entity列表
     * @param entityType
     * @param userId
     * @param offset
     * @param num
     * @return
     */
    public List<Integer> getFollowees(int entityType, int userId, int offset, int num){
        String followeesKey = RedisKeyUtil.get(userId, entityType, RedisKeyType.FOLLOWEE);
        return jedisDAO.zrevrange(followeesKey, offset, offset + num);
    }

    /**
     * 获得粉丝列表
     * @param entityType
     * @param entityId
     * @param offset
     * @param num
     * @return
     */
    public List<Integer> getFollowers(int entityType, int entityId, int offset, int num){
        String followersKey = RedisKeyUtil.get(entityId, entityType, RedisKeyType.FOLLOWER);
        return jedisDAO.zrevrange(followersKey, offset, offset + num);
    }
}
